package com.hzg.lanya.frame;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.hzg.lanya.R;

import java.util.HashMap;
import com.github.lzyzsd.circleprogress.DonutProgress;
public class HeartFragment extends Fragment implements View.OnClickListener {
    private final String TAG = "HeartFragment";
    private String address="";

    private static final int REQ_HEX_INPUT = 3;
    private EditText edt1;
    private EditText edt2;
    private EditText edt3;
    private TextView nametable;


    public void displayRxData(String dataStr) {

        //Log.i("Flog","接收到消息="+dataStr);
            //dataStr += "收到数据: " + DataUtil.byteArrayToHex(data) + '\n';

           // Toast.makeText(getActivity(),"收到数据："+dataStr, Toast.LENGTH_SHORT).show();

            if (dataStr!=null && dataStr.length()>=16){
                String host=dataStr.substring(0,4);
                if (host.equals("A0AA")){
                    setTextTable("设置时间成功");
                }else if (host.equals("A0A1")){
                    setTextTable("设置电流成功");
                }else {
                    setTextTable("失败，未知指令:"+dataStr);
                }
            }else {
                setTextTable("失败:"+dataStr);
            }
    }



    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        zcMyGb();
//        mLeProxy = LeProxy.getInstance();
//        LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mLocalReceiver, makeFilter());


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.layout_heart, container, false);






        initView(view);
        return view;
    }

    boolean cboc1=false;
    private void initView(View view) {

        DonutProgress outerCircleProgress = view.findViewById(R.id.outer_circle_progress);
        DonutProgress innerCircleProgress = view.findViewById(R.id.inner_circle_progress);
        TextView textTop = view.findViewById(R.id.text_top);
        TextView textBottom = view.findViewById(R.id.text_bottom);

// 设置圆形进度条数据
        outerCircleProgress.setProgress(127);
        innerCircleProgress.setProgress(3231);

        outerCircleProgress.setFinishedStrokeColor(Color.parseColor("#FF0000"));
        innerCircleProgress.setFinishedStrokeColor(Color.parseColor("#00c1c1"));
        outerCircleProgress.setTextColor(Color.TRANSPARENT);
        innerCircleProgress.setTextColor(Color.TRANSPARENT);
// 设置上半部分文本
        textTop.setText("127");
        textTop.setTextColor(Color.RED); // 设置文本颜色为白色，以便在进度条上清晰显示

// 设置下半部分文本
        textBottom.setText("3231");
        textBottom.setTextColor(Color.parseColor("#00FFFF")); // 设置文本颜色为白色，以便在进度条上清晰显示


        // 设置列表数据
        TextView walkTime = view.findViewById(R.id.walk_time);
        walkTime.setText("50 Min");
        TextView runTime = view.findViewById(R.id.run_time);
        runTime.setText("0 Min");
        TextView cyclingTime = view.findViewById(R.id.cycling_time);
        cyclingTime.setText("0 Min");

//        nametable=(TextView) view.findViewById(R.id.nametable);
//
//        //发送数据
//        view.findViewById(R.id.abtn1).setOnClickListener(this);
//        view.findViewById(R.id.abtn2).setOnClickListener(this);
//        view.findViewById(R.id.abtn3).setOnClickListener(this);
//        view.findViewById(R.id.abtn4).setOnClickListener(this);
//        view.findViewById(R.id.abtn5).setOnClickListener(this);
//        view.findViewById(R.id.abtn6).setOnClickListener(this);
//
//        view.findViewById(R.id.bbtn1).setOnClickListener(this);
//        view.findViewById(R.id.bbtn2).setOnClickListener(this);
//        view.findViewById(R.id.bbtn3).setOnClickListener(this);
//        view.findViewById(R.id.bbtn4).setOnClickListener(this);
//        view.findViewById(R.id.bbtn5).setOnClickListener(this);
//        view.findViewById(R.id.bbtn6).setOnClickListener(this);
//
//        view.findViewById(R.id.sj_send).setOnClickListener(this);
//        view.findViewById(R.id.dl_send).setOnClickListener(this);
//        view.findViewById(R.id.txtset).setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {


    }

    int count=0;
    private void countStort(){
        count++;
        if (count>10){
            setMaxDl();
        }
    }

    /**
     * 设置最大电流
     */
    private void setMaxDl(){
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        View view =getLayoutInflater().inflate(R.layout.layout_dialog,null);
        final EditText edt=(EditText) view.findViewById(R.id.dialog_edt);
        Button b=(Button)view.findViewById(R.id.dialog_btn);

        builder.setView(view);
        builder.setTitle("设置电流最大值");
        final AlertDialog dialog = builder.create();//创建
        dialog.show();
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String s=edt.getText().toString();
                edt3.setText(s);
                dialog.dismiss();
            }
        });
    }

    // 向勾选的设备发送数据
    private void send(String inputStr) {
        //MyConfig.SEND_MSG=inputStr;
        MyConfig.mScanFragment.send(inputStr);
        //Toast.makeText(getActivity(),"发送："+inputStr, Toast.LENGTH_SHORT).show();

    }



    private String name="未连接";
    @Override
    public void onResume() {
        super.onResume();
//        Log.i(TAG, "onResume()");
//        Log.i("Flog", "---");
//        name = MyConfig.name;
//        address = MyConfig.address;
//        setTextTable("");
    }

    @Override
    public void onStart() {
        super.onStart();
        count=0;
        Log.i("Flog", "---");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        getActivity().unregisterReceiver(bootCompleteReceiver);//自己定义收消息的广播
        Log.i("Flog","销毁数据配置页面");
    }

    private void setTextTable(String str){
        nametable.setText(name+"      "+str);
    }


    /**
     * 合成设置时间质指令
     * @param i
     * @return
     */
    private String shiJianZL(int i){
        String str="AAAA";
        String hex=utils.intToHex(i);
        int a=12-hex.length();
        for (int j=0;j<a;j++){
            str=str+"0";
        }
        str=str+hex;
        return str;
    }
    /**
     * 合成设置电流指令质指令
     * @param i
     * @return
     */
    private String dianLiuZL(int i){
        String str="A1A1";
        String hex=utils.intToHex(i);
        int a=12-hex.length();
        for (int j=0;j<a;j++){
            str=str+"0";
        }
        str=str+hex;
        return str;
    }




    /**
     * 自己定义收消息的广播
     */
    public BootCompleteReceiver bootCompleteReceiver=new BootCompleteReceiver();
    private void zcMyGb(){
        IntentFilter intentFilter=new IntentFilter(MyConfig.MY_MSG_GB);
        getActivity().registerReceiver(bootCompleteReceiver,intentFilter);
    }

    public class BootCompleteReceiver extends BroadcastReceiver {
        public BootCompleteReceiver(){

        }

        @Override
        public void onReceive(Context context, Intent intent) {//下面的语句根据自己的需求来写
            String a=intent.getAction();
            if (a.equals(MyConfig.MY_MSG_GB)){
                String name=intent.getStringExtra("key");
                if (MyConfig.SEND_MSG.equals(name) || name.length()<5)return;
                name=name.replace(" ","");
                //Toast.makeText(context, "收到数据："+name, Toast.LENGTH_SHORT).show();
                Log.i("Flog88","conn收到消息："+name);
                displayRxData(name);
            }

        }
    }


}